package cn.doctor.assistant.pad.ui.huifang;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.doctor.assistant.pad.R;
import cn.doctor.assistant.pad.api.huifang.AddFollowedRequest;
import cn.doctor.assistant.pad.api.huifang.ReCallApi;
import cn.doctor.assistant.pad.base.BaseNetActivity;
import cn.doctor.assistant.pad.base.ResultCallback;
import cn.doctor.assistant.pad.entity.FollowedBean;
import cn.doctor.assistant.pad.entity.Record;
import cn.doctor.assistant.pad.ui.SpinnerHelper;

/**
 * 新增回访记录
 */
public class AddRecallActivity extends BaseNetActivity {

    @InjectView(R.id.titlebar_left_title)
    TextView titlebarLeftTitle;
    @InjectView(R.id.titlebar_right_text)
    TextView titlebarRightText;
    @InjectView(R.id.icon)
    ImageView icon;
    @InjectView(R.id.text_name)
    TextView textName;
    @InjectView(R.id.edit_recall_date)
    EditText editRecallDate;
    @InjectView(R.id.edit_recall_note)
    EditText editRecallNote;
    @InjectView(R.id.spinner_recall_result)
    Spinner spinnerRecallResult;
    @InjectView(R.id.spinner_recall_manyidu)
    Spinner spinnerRecallManyidu;

    String[] ITEMS_RESULT = {"成功回访","电话未接通"};
    String[] ITEMS_MANYIDU = {"非常满意", "一般", "不满意"};

    Record mRecord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_recall);
        ButterKnife.inject(this);
        setTitle("新增回访记录");
        setRightTitleBarText("保存");
        mRecord = (Record) getIntent().getExtras().getSerializable("record");
        initialize();
    }

    void initialize() {
        textName.setText(mRecord.getName());
        SpinnerHelper.init(spinnerRecallResult, ITEMS_RESULT, null);
        SpinnerHelper.init(spinnerRecallManyidu, ITEMS_MANYIDU, null);

        editRecallDate.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    setRecallDate();
                    return true;
                }
                return true;
            }
        });
    }

    void save() {
        String date = editRecallDate.getText().toString();
        String note = editRecallNote.getText().toString().trim();

        if (TextUtils.isEmpty(date)) {
            showToast("请填写回访日期");
            return;
        }

        if (TextUtils.isEmpty(note)) {
            showToast("请填写回访记录");
            return;
        }

        final AddFollowedRequest addRecordRequest = new AddFollowedRequest();
        addRecordRequest.setRecord_id(mRecord.getId());
        addRecordRequest.setContent(note);
        addRecordRequest.setDate(date);
        addRecordRequest.setDegree(ITEMS_MANYIDU[spinnerRecallManyidu.getSelectedItemPosition()]);
        addRecordRequest.setResult(ITEMS_RESULT[spinnerRecallResult.getSelectedItemPosition()]);
        ReCallApi.addFollowed(addRecordRequest, new ResultCallback<String>() {
            @Override
            public void onSuccess(String result) {
                showToast(result);

                Intent intent = new Intent();
                FollowedBean fb = new FollowedBean();
                fb.setContent(addRecordRequest.getContent());
                fb.setDate(addRecordRequest.getDate());
                intent.putExtra("followed", fb);
                setResult(RESULT_OK, intent);

                titlebarRightText.setVisibility(View.GONE);
                editRecallDate.setEnabled(false);
                editRecallNote.setEnabled(false);
                spinnerRecallResult.setEnabled(false);
                spinnerRecallManyidu.setEnabled(false);
            }
        });
    }

    @OnClick({R.id.titlebar_left_title, R.id.titlebar_right_text})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_left_title:
                back();
                break;
            case R.id.titlebar_right_text:
                save();
                break;
        }
    }

    String birthday;
    String birthdayDate; // 格式 "Apr 1, 2016 3:01:24 PM"
    void setRecallDate() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int monthOfYear = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH) + 1;

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                birthday = String.format("%s-%02d-%02d", year, monthOfYear + 1, dayOfMonth);
                editRecallDate.setText(birthday);

                Date date = new Date();
                date.setYear(year - 1900);
                date.setMonth(monthOfYear);
                date.setDate(dayOfMonth);
//                birthdayDate = date.toString();
                birthdayDate = String.format(Locale.US, "%tb %d, %d 00:00:00", date, dayOfMonth, year);
                //UI.showToast(birthdayDate);

            }

        }, year, monthOfYear, dayOfMonth).show();
    }

}
